package com.msmall.community.bbs.action.wml;

import java.util.List;

import com.msmall.community.bbs.model.Board;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.BoardManager;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.bbs.util.BbsUtil;
import com.msmall.community.common.util.FilterCharsUtil;

public class TopicAction extends BBSBaseAction{

	private static final long serialVersionUID = -2748124318500254047L;

	private int result;
	
	private Board board;

	private Topic topic;
	
	/**
	 * 回复发表人
	 */
	private Long threadPoster;
	
	/**
	 * 判断是不是第一次点击主题帖 Y:是
	 */
	private String firstClick; 
	
	private List<Thread> threads;

	private TopicManager topicManager;
	
	private BoardManager boardManager;
	/**
	 * 进入创建帖子页面
	 * @return
	 */
	public String create(){
		board = boardManager.getBoard(boardId);
		return "create";
	}
	/**
	 * 保存帖子，用于创建和修改
	 * @return
	 */
	public String save(){
		if(validateTopic()){
			topic.setNickname(getNickname());
			topicManager.saveTopic(topic, boardId, getUserid(), userAgentType );
			String toUrl = "boardIndex.do?boardId=" + boardId;
			this.setBbsToUrl(toUrl);
			return "save";
		}else{
			return INPUT;
		}
	}

	/**
	 * 查看帖子, 增加点击数
	 * @return
	 */
	public String view(){
		
		return "view";
	}

	/**
	 * 只看楼主，显示主题帖和回复
	 * @return
	 */
	public String viewTopicOwner(){
		
		return "view";
	}
	
	/**
	 * 只看他，显示主题帖和回复
	 * @return
	 */
	public String viewTopicWho(){
		
		return "view";
	}
	
	public boolean validateTopic(){
		board = boardManager.getBoard(boardId);
		String title = topic.getTitle();
		int titleOK = BbsUtil.checkTopicTitleLength(title);
		if(titleOK == 1){
			addMessage(1, getText("wml.bbs.topic.create.error1"));
			return false;
		}
		if(titleOK == 2){
			addMessage(1, getText("wml.bbs.topic.create.error2"));
			return false;
		}
		boolean titleC = FilterCharsUtil.containsFilterChars(title);
		if(titleC){
			addMessage(1, getText("wml.bbs.topic.create.error3"));
			return false;
		}
		
		String content = topic.getContent();
		int contentOK = BbsUtil.checkTopicContentLength(content);
		if(contentOK == 1){
			addMessage(1, getText("wml.bbs.topic.create.error4"));
			return false;
		}
		if(contentOK == 2){
			addMessage(1, getText("wml.bbs.topic.create.error5"));
			return false;
		}
		boolean contentC = FilterCharsUtil.containsFilterChars(content);
		if(contentC){
			addMessage(1, getText("wml.bbs.topic.create.error6"));
			return false;
		}
		return true;
	}
	public int getResult() {
		return result;
	}
	public void setResult(int result) {
		this.result = result;
	}
	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public Long getThreadPoster() {
		return threadPoster;
	}
	public void setThreadPoster(Long threadPoster) {
		this.threadPoster = threadPoster;
	}
	public String getFirstClick() {
		return firstClick;
	}
	public void setFirstClick(String firstClick) {
		this.firstClick = firstClick;
	}
	public List<Thread> getThreads() {
		return threads;
	}

	public void setThreads(List<Thread> threads) {
		this.threads = threads;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}
	
	public void setBoardManager(BoardManager boardManager) {
		this.boardManager = boardManager;
	}
}
